package commons;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.codeandmagic.android.gauge.demo.R;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class DataUlities {
	private Handler mHandler = new Handler();
	private Runnable mTimer;
	private Context mContext;
	private WindowManager windowManager;
	private ImageView chatHead;
	private WindowManager.LayoutParams params;
	private View v;
	private static boolean display = true;
	int count;
	MediaPlayer mp;

	public DataUlities(Context ctx) {
		mContext = ctx;
		mTimer = new Runnable() {
			public void run() {
				count++;
				mHandler.postDelayed(this, 1000);
				if (count % 2 == 0) {
					try {
						windowManager.addView(v, params);
					} catch (Exception e) {
					}
				} else {
					try {
						windowManager.removeView(v);
					} catch (Exception e) {
					}
				}
			}
		};
	}

	public void showNotification(String title, String text) {
		Intent notifyIntent = new Intent();
		PendingIntent notifyPenIntent = PendingIntent.getActivity(mContext, 0,
				notifyIntent, 0);

		NotificationCompat.Builder _mNoti = new NotificationCompat.Builder(
				mContext).setSmallIcon(R.drawable.ic_launcher)
				.setContentIntent(notifyPenIntent)
				.setTicker("You have new message!").setAutoCancel(true)
				.setContentTitle(title).setContentText(text);

		NotificationManager _mNotiManager = (NotificationManager) mContext
				.getSystemService(Context.NOTIFICATION_SERVICE);
		_mNotiManager.notify((int) System.currentTimeMillis(), _mNoti.build());
	}

	public void autoSendSMS(String number, String text) {
		try {
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(number, null, text, null, null);
			Toast.makeText(mContext, "SMS Sent!", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Toast.makeText(mContext, "SMS faild, please try again later!",
					Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}

	public void stopWarning() {
		if (mTimer != null) {
			mHandler.removeCallbacks(mTimer);
		}

	}

	public String getData(String method) {
		String result = "";
		try {
			if (method.equals("find")) {
				result = new AsyncTaskPage(mContext, "find")
						.execute(
								"http://10.3.9.147:1337/dataserie/findbyuseranddevice/user1/device1")
						.get();
			} else {
				result = new AsyncTaskPage(mContext, "useranddevice").execute(
						"http://10.3.9.147:1337/" + method).get();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			Toast.makeText(mContext, "@@", 1000).show();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			Toast.makeText(mContext, "@@", 1000).show();
		}
		return result;
	};

	public List<String> convertJSON(String method) {
		List<String> list = new ArrayList<String>();
		JSonConvert jConvert = new JSonConvert(mContext, getData(method));
		if (method.equals("user")) {
			list = jConvert.getUser();
		}
		if (method.equals("device")) {
			list = jConvert.getDevice();
		}
		return list;
	}

	public void callAnother() {
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:09999"));
		mContext.startActivity(intent);
	}

	private void playSound() {
		mp = MediaPlayer.create(mContext, R.raw.sound);
		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				try {
					mHandler.removeCallbacks(mTimer);
					windowManager.removeView(v);
					callAnother();
				} catch (Exception e) {
					display = true;
				}
			}
		});
	}

	public void showWarning() {
		if (display == true) {
			display = false;
			windowManager = (WindowManager) mContext
					.getSystemService(Context.WINDOW_SERVICE);
			LayoutInflater inflat = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflat.inflate(R.layout.warning, null);

			params = new WindowManager.LayoutParams(
					WindowManager.LayoutParams.WRAP_CONTENT,
					WindowManager.LayoutParams.WRAP_CONTENT,
					WindowManager.LayoutParams.TYPE_PHONE,
					WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
					PixelFormat.TRANSLUCENT);

			params.gravity = Gravity.CENTER;
			params.x = 0;
			params.y = 100;

			windowManager.addView(v, params);
			mHandler.postDelayed(mTimer, 0);
			playSound();
		} else {

		}
		// showNotification("Warning", "Warning");
	}

}
